﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);
                
                LocalizeControls();

                BindNewsBox();
                BindRecentSales();
                BindRecentPurchase();
            }

            BindIntroTexts();
        }

        /// <summary>
        /// Binds the introductory texts of the page.
        /// </summary>
        private void BindIntroTexts()
        {
            string fileName = Path.GetFileName(Request.Path);

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            Dictionary<string, string> informationMap = new Dictionary<string, string>();

            if (culture.Equals("en-GB"))
                informationMap = dbInformation.GetInformationByPageId(fileName, culture);
            else
                informationMap = dbInformation.GetInformationByPageId(fileName);

            lblIntroText.Text = informationMap["lblIntroText"];

            lblPurchaseText.Text = informationMap["lblPurchaseText"];

            lblSaleText.Text = informationMap["lblSaleText"];
        }

        /// <summary>
        /// Binds the 3 latest news articles to the news box.
        /// </summary>
        private void BindNewsBox()
        {
            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            List<News> news = new List<News>();
            
            if(culture.Equals("en-GB"))
                news = dbNews.GetTop3News(culture);
            else
                news = dbNews.GetTop3News();

            dlNewsBox.DataSource = news;
            dlNewsBox.DataBind();
        }

        /// <summary>
        /// Binds the a specified amount of latest sales, the amount is based on profile settings if logged in and a default value if not.
        /// </summary>
        private void BindRecentSales()
        {
            repRecentSales.DataSource = dbExchange.GetRecentSales(5);
            repRecentSales.DataBind();
        }

        /// <summary>
        /// Binds the a specified amount of latest purchases, the amount is based on profile settings if logged in and a default value if not.
        /// </summary>
        private void BindRecentPurchase()
        {
            repRecentPurchase.DataSource = dbExchange.GetRecentPurchases(2);
            repRecentPurchase.DataBind();
        }

        // Event fires when the datalist is DataBound
        protected void dlNewsBox_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Separator)
            {
                //Truncates the text to fit the news box
                Label lblNewsArchiveText = (Label)e.Item.FindControl("lblNewsArchiveText");
                lblNewsArchiveText.Text = GeneralTools.TruncateString(lblNewsArchiveText.Text, 100, ".");
            }
        }

        // Event fires when the repeater is DataBound
        protected void repRecentPurchase_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //Truncates the title to fit the box
            HyperLink hyLinkRecentPurchaseTitle = (HyperLink)e.Item.FindControl("hyLinkRecentPurchaseTitle");
            hyLinkRecentPurchaseTitle.Text = GeneralTools.TruncateString(hyLinkRecentPurchaseTitle.Text, 20, " ") + "...";
        }

        // Event fires when the repeater is DataBound
        protected void repRecentSales_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //Truncates the title to fit the box
            HyperLink hyLinkRecentSalesTitle = (HyperLink)e.Item.FindControl("hyLinkRecentSalesTitle");
            hyLinkRecentSalesTitle.Text = GeneralTools.TruncateString(hyLinkRecentSalesTitle.Text, 20, " ") + "...";
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnPurchase.Text = GetLocalResourceObject("btnPurchaseText").ToString();
            btnSale.Text = GetLocalResourceObject("btnSaleText").ToString();
            lblIntroHeader.Text = GetLocalResourceObject("lblIntroHeaderText").ToString();
            lblPurchaseHeader.Text = GetLocalResourceObject("lblPurchaseHeaderText").ToString();
            lblRecentPurchasesHeader.Text = GetLocalResourceObject("lblRecentPurchasesHeaderText").ToString();
            lblRecentSaleHeader.Text = GetLocalResourceObject("lblRecentSaleHeaderText").ToString();
            lblSaleHeader.Text = GetLocalResourceObject("lblSaleHeaderText").ToString();
            lblNewsBoxHeader.Text = GetLocalResourceObject("lblNewsBoxHeaderText").ToString();
        }
    }
}